﻿using Daemon.Model;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Daemon.Common;
using Daemon.Common.Cache;
using Daemon.Common.Exceptions;
using System.Net;

namespace DaemonAPI
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ProjectInfoController : BaseApiController<ProjectInfo, IProjectInfoRepository>
    {
        private readonly IDaemonDistributedCache _daemonDistributedCache;

        public ProjectInfoController(IProjectInfoRepository repository, IHttpContextAccessor httpContextAccessor, IDaemonDistributedCache daemonDistributedCache) : base(repository, httpContextAccessor)
        {
            _daemonDistributedCache = daemonDistributedCache;
        }

        [Route("")]
        [HttpGet]
        public ResultModel<ProjectInfo, IProjectInfoRepository> ChooseProject(int projectId)
        {
            var projectInfo = _repository.FindById(projectId);
            if (projectInfo == null)
            {
                throw new BusinessException((int)BusinessCode.NonProject, "当前项目不存在！");
            }

            _daemonDistributedCache.Set($"{UserId}_ProjectId", projectId);
            return new ResultModel<ProjectInfo, IProjectInfoRepository>(HttpStatusCode.OK, "", projectInfo);
        }
    }
}
